from selenium import webdriver
import time
import redis


def update_token():
    redis_conn = redis.Redis(host='localhost', port=6379, db=0, decode_responses=True)
    options = webdriver.ChromeOptions()
    options.add_argument("--user-data-dir=C:/Users/20486/AppData/Local/Google/Chrome/User Data/Default")
    options.add_experimental_option('excludeSwitches', ['enable-automation'])
    options.add_argument("--disable-blink-features=AutomationControlled")
    driver = webdriver.Chrome(options=options)
    driver.get("https://s.taobao.com/search?_input_charset=utf-8&commend=all&ie=utf8&initiative_id=tbindexz_20170306&page=2&preLoadOrigin=https%3A%2F%2Fwww.taobao.com&q=%E9%85%8D%E9%A5%B0&search_type=item&source=suggest&sourceId=tb.index&spm=a21bo.jianhua%2Fa.search_history.d1&ssid=s5-e&suggest_query=&tab=all&wq=")
    driver.maximize_window()
    time.sleep(10)
    _m_h5_tk = driver.get_cookie("_m_h5_tk")["value"]
    _m_h5_tk_enc = driver.get_cookie("_m_h5_tk_enc")["value"]
    print("_m_h5_tk:", _m_h5_tk)
    print("_m_h5_tk_enc", _m_h5_tk_enc)
    redis_conn.set("_m_h5_tk", _m_h5_tk)
    redis_conn.set("_m_h5_tk_enc", _m_h5_tk_enc)
    driver.close()


update_token()
